package stream流;


import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class streamFilter {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4, 5, 6};
//        Arrays.stream(arr1).forEach(System.out::println);

        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"张无忌","周芷若","赵敏","赵敏","张强","张三丰","张翠山");
        list.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                //如果返回值为true,表示当前数据留下
                //如果返回值为false,表示当前数据舍弃
                return s.startsWith("张");
            }
        });

//        list.stream()
//                .filter(s -> s.startsWith("张"))
//                .filter(s -> s.length()==3).forEach(s -> System.out.println(s));
        list.stream().limit(2).forEach(System.out::println);
        System.out.println("===================");
        list.stream().skip(2).forEach(System.out::println);
        System.out.println("===================");
        list.stream().distinct().forEach(System.out::println);
        System.out.println("===================");

        List<String> list1 = new ArrayList<>();
        Collections.addAll(list1, "李武","麻溜");
        //流的合并
        Stream.concat(list.stream(),list1.stream()).forEach(System.out::println);

        List<String> list2 = new ArrayList<>();
        Collections.addAll(list2,"张三-15","李四-20");
//        list2.stream().map(new Function<String, Integer>() {
//            @Override
//            public Integer apply(String s) {
//                //形参s表示流里面的每一个参数
//                String[] arr = s.split("-");
//                return Integer.valueOf(arr[1]);
//            }
//        }).forEach(System.out::println);
        list2.stream().map(s -> Integer.valueOf(s.split("-")[1]))
                .forEach(System.out::println);
        long count = list2.stream().count();
        System.out.println(count);

        //流里面的数据，放到数组中
//        String[] strings = list2.stream().toArray(new IntFunction<String[]>() {
//            @Override
//            public String[] apply(int value) {
//                return new String[value];
//            }
//        });

        String[] strings = list2.stream().toArray(s->new String[s]);
        System.out.println(Arrays.toString(strings));
        for (String string : strings) {
            System.out.println(string);
        }

        // 收集结果到List中
        List<String> arrList = list2.stream().filter(s -> s.startsWith("张")).collect(Collectors.toList());
        System.out.println(arrList.toString());

        // 收集结果到Set中
        Set<String> arrSet = list2.stream().filter(s -> s.startsWith("张")).collect(Collectors.toSet());
        System.out.println(arrSet.toString());
        System.out.println(arrSet.size());

        //收集到Map中
        List<String> list3 = new ArrayList<>();
        Collections.addAll(list3,"张三-男-15","李四-男-20","赵敏-女-23");
//        Map<String, Integer> collect = list3.stream().filter(s -> "男".equals(s.split("-")[1]))
//                .collect(Collectors.toMap(new Function<String, String>() {
//                    @Override
//                    public String apply(String s) {
//                        return s.split("-")[0];
//                    }
//                }, new Function<String, Integer>() {
//                    @Override
//                    public Integer apply(String s) {
//                        return Integer.valueOf(s.split("-")[2]);
//                    }
//                }));
//        System.out.println(collect.toString());
        Map<String, String> collect = list3.stream()
                .filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toMap(
                        s -> s.split("-")[0],
                        s -> s.split("-")[2]));
        System.out.println(collect.toString());
    }
}
